package com.xixilala.video.render

import android.opengl.GLES20
import android.opengl.GLSurfaceView
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import java.nio.IntBuffer
import java.util.*
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10
import kotlin.collections.ArrayList

/***
 * https://developer.android.google.cn/training/graphics/opengl/draw?hl=zh-cn 参考
 *
 * 1.createProgram
 * 2.0createVertextShader
 * 2.1sourceVertexShader
 * 2.2compileVertexShader
 * 3.0createFragmentShader
 * 3.1sourceFragmentShader
 * 3.2compileVertexShader
 * 4.attachVertexShader
 * 5.attachFragmentShader
 * 6.linkProgram
 * 7.usePrgram
 * 8.0getAttriLocation
 * 8.1enableVertexAttriArray
 * 8.2setVertexs
 * 9.0getUniformLocation
 * 9.1setColor
 * 10.drawArray
 */
class TriangleSimple1Render :GLSurfaceView.Renderer{

    private lateinit var triangle:Triangle

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        triangle = Triangle()
        GLES20.glClearColor(0f,1f,0f,0.5f)
    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES20.glViewport(0,0,width,height)
    }

    override fun onDrawFrame(gl: GL10?) {
        GLES20.glClearColor(0f,1f,1f,0.5f)
        triangle.draw()
    }

    class Triangle {
        private var mProgram:Int = GLES20.glCreateProgram()
        private var mPositionHandle:Int = 0
        private var mColorHandle: Int = 0

        /*********创建三角形********/
        private val COORDS_PER_VERTEX = 3 //每个顶点座标数
        var triangleCoords = floatArrayOf(     // in counterclockwise order:
            0.0f, 0.622008459f, 0.0f,      // top
            -0.5f, -0.311004243f, 0.0f,    // bottom left
            0.5f, -0.311004243f, 0.0f      // bottom right
        )

        //设置颜色
        val color = floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f)

        var vertexBuffer:FloatBuffer = ByteBuffer.allocateDirect(triangleCoords.size*4).run {
            order(ByteOrder.nativeOrder())

            asFloatBuffer().apply {

                put(triangleCoords)

                position(0)
            }
        }
        /*****************/

        private val vertexStride: Int = COORDS_PER_VERTEX * 4 // 4 bytes per vertex
        private val vertexCount: Int = triangleCoords.size / COORDS_PER_VERTEX

        private val vertexShaderCode =
                "attribute vec4 vPosition;" +
                        "void main() {" +
                        "  gl_Position = vPosition;" +
                        "}"

        private val fragmentShaderCode =
                "precision mediump float;" +
                        "uniform vec4 vColor;" +
                        "void main() {" +
                        "  gl_FragColor = vColor;" +
                        "}"

        init {
            val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER)
            GLES20.glShaderSource(vertexShader,vertexShaderCode)
            GLES20.glCompileShader(vertexShader)

            val fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER)
            GLES20.glShaderSource(fragmentShader,fragmentShaderCode)
            GLES20.glCompileShader(fragmentShader)

            GLES20.glAttachShader(mProgram,vertexShader)
            GLES20.glAttachShader(mProgram,fragmentShader)

            GLES20.glLinkProgram(mProgram)
        }

        fun draw() {
            GLES20.glUseProgram(mProgram)

            // 拿到vertexShaderCode中定义的 vPosition 句柄
            mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition")
            // 开启顶点
            GLES20.glEnableVertexAttribArray(mPositionHandle)
            // 设置顶点数据
            GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer)

            mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor")

            GLES20.glUniform4fv(mColorHandle,1,color,0)

            GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount)

//            GLES20.glDisableVertexAttribArray(mPositionHandle)


        }
    }
}